package com.shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;
import java.util.List;

import com.shop.common.DButils;
import com.shop.entity.admin;
/**
 * 
 * @类名 adminDaoImpl
 * @描述 TODO
 * @作者： 李健玖
 * @日期：2018年6月28日下午2:46:53
 */
public class adminDaoImpl implements adminDaoI {

	public boolean insert(admin admin) throws Exception {
		Connection conn=DButils.getConnection();
		String sql="INSERT INTO `admin`(aid,adminname,password,phone,sex,birthday,roleId,creatDate,lastDate)VALUES(?,?,?,?,?,?,?,?,?)";
		//创建PreparedStatement
		PreparedStatement ps=conn.prepareStatement(sql);
		ps.setString(1, admin.getAid());
		ps.setString(2, admin.getAdminname());
		ps.setString(3, admin.getPassword());
		ps.setString(4, admin.getPhone());
		ps.setString(5, admin.getSex());
		if(null!=admin.getBirthday()){
		ps.setDate(6, new Date(admin.getBirthday().getTime()));
		}else{
			ps.setDate(6, null);
		}
		ps.setString(7, admin.getRoleId());
		if(null!=admin.getCreatDate()){
		ps.setDate(8, new Date(new java.util.Date().getTime()));
		}else{
			ps.setDate(8, null);
		}
		if(null!=admin.getLastDate()){
		ps.setDate(9, new Date(admin.getLastDate().getTime()));
		}else{
			ps.setDate(9, null);
		}
		//执行语句
		if(ps.executeUpdate()>0){
			return true;
		}
		return false;
	}

	public boolean update(admin admin) throws Exception {
		Connection conn=DButils.getConnection();
		String sql="UPDATE `admin` SET adminname=?,password=?,phone=?,sex=?,birthday=?,roleId=?,creatDate=?,lastDate=?WHERE aid=?";
		//创建PreparedStatement
		PreparedStatement ps=conn.prepareStatement(sql);
		ps.setString(1, admin.getAdminname());
		ps.setString(2, admin.getPassword());
		ps.setString(3, admin.getPhone());
		ps.setString(4, admin.getSex());
		if(null!=admin.getBirthday()){
		ps.setDate(5, new Date(admin.getBirthday().getTime()));
		}else{
			ps.setDate(5, null);
		}
		ps.setString(6, admin.getRoleId());
		if(null!=admin.getCreatDate()){
		ps.setDate(7, new Date(new java.util.Date().getTime()));
		}else{
			ps.setDate(7, null);
		}
		if(null!=admin.getLastDate()){
		ps.setDate(8, new Date(admin.getLastDate().getTime()));
		}else{
			ps.setDate(8, null);
		}
		ps.setString(9, admin.getAid());
		//执行语句
		if(ps.executeUpdate()>0){
			return true;
		}
		return false;
	}

	public boolean delete(int aid) {
		// TODO Auto-generated method stub
		return false;
	}

	public List<admin> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

}
